package com.ccsoft.zknote.util;

import com.cc.commontools.entity.DirectoryNode;
import com.cc.commontools.entity.NaiveTreeNode;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 12月18日 本项目专用的工具类，非通用
 */
public class ResourcesFileUtils {
    private static int fileLevel;

    public ResourcesFileUtils() {
    }

    public static void getAllFoldersAndFiles(String path) {
        try {
            Stream<Path> paths = Files.walk(Paths.get(path));
            Throwable var2 = null;

            try {
                List<Path> fileNames = (List)paths.filter((x$0) -> {
                    return Files.isRegularFile(x$0, new LinkOption[0]);
                }).collect(Collectors.toList());
                System.out.println(fileNames.toString());
            } catch (Throwable var12) {
                var2 = var12;
                throw var12;
            } finally {
                if (paths != null) {
                    if (var2 != null) {
                        try {
                            paths.close();
                        } catch (Throwable var11) {
                            var2.addSuppressed(var11);
                        }
                    } else {
                        paths.close();
                    }
                }

            }
        } catch (IOException var14) {
            var14.printStackTrace();
        }

    }

    public static void buildTree(String dirPath, DirectoryNode fatherNode) {
        File file = new File(dirPath);
        File[] list = file.listFiles();
        if (list != null) {
            for(int i = 0; i < list.length; ++i) {
                DirectoryNode node = new DirectoryNode();
                node.setName(list[i].getName());
                if (list[i].isDirectory()) {
                    node.setType(0);
                    buildTree(list[i].getPath(), node);
                } else {
                    node.setType(1);
                }

                fatherNode.getChildren().add(node);
            }
        }

    }

    public static void buildTree(String dirPath, DirectoryNode fatherNode, NaiveTreeNode naiveTreeNode) throws FileNotFoundException {
        //File file = new File(dirPath);
        System.out.println("进入buildTree，当前路径：" + dirPath);
        File file = ResourceUtils.getFile(dirPath);
        System.out.println("通过ResourceUtils读取到的数据：" + file.toString());
        File[] list = file.listFiles();
        System.out.println("fileLevel:"+fileLevel+"通过 file.listFiles() 读取到的数据长度：" + list.length);
        if (list != null && list.length > 0) {
            for(int i = 0; i < list.length; ++i) {
                DirectoryNode node = new DirectoryNode();
                node.setName(list[i].getName());
                NaiveTreeNode naiveTreeNode1 = new NaiveTreeNode();
                String nodeKey = list[i].getName() + "-" + fileLevel;
                naiveTreeNode1.setLabel(list[i].getName());
                String fatherKey = (String) Optional.ofNullable(naiveTreeNode.getKey()).orElse("");
                nodeKey = nodeKey + "-" + fatherKey;
                naiveTreeNode1.setKey(nodeKey);
                if (list[i].isDirectory()) {
                    node.setType(0);
                    naiveTreeNode1.setLeaf(false);
                    ++fileLevel;
                    buildTree(list[i].getPath(), node, naiveTreeNode1);
                    --fileLevel;
                } else {
                    node.setType(1);
                    naiveTreeNode1.setLeaf(true);
                }

                fatherNode.getChildren().add(node);
                naiveTreeNode.getChildren().add(naiveTreeNode1);
            }
        }

    }

    public static void printSubFiles(String dirPath) {
        File file = new File(dirPath);
        File[] list = file.listFiles();

        for(int i = 0; i < list.length; ++i) {
            if (list[i].isDirectory()) {
                System.out.println(createPrintStr(list[i].getName(), fileLevel));
                ++fileLevel;
                printSubFiles(list[i].getPath());
                --fileLevel;
            } else {
                System.out.println(createPrintStr(list[i].getName(), fileLevel));
            }
        }

    }

    public static String createPrintStr(String name, int level) {
        String printStr = "";

        for(int i = 0; i < level; ++i) {
            printStr = printStr + "  ";
        }

        printStr = printStr + "- " + name;
        return printStr;
    }

    public static void printFatherDir(String dirPath) {
        String[] dirNameList = dirPath.split("\\\\");
        fileLevel = dirNameList.length;

        for(int i = 0; i < dirNameList.length; ++i) {
            System.out.println(createPrintStr(dirNameList[i], i));
        }

    }

    public static String getProjRootDirectory() {
        return System.getProperty("user.dir");
    }

    public static void main(String[] args) throws FileNotFoundException {
        //String rootPath = "D:\\source\\java\\zknotebe\\src\\main\\resources\\templates";
        String rootPath = "classpath:templates";
        DirectoryNode rootNode = new DirectoryNode();
        NaiveTreeNode naiveTreeNode = new NaiveTreeNode();
        naiveTreeNode.setLabel("root");
        naiveTreeNode.setKey("root");
        buildTree(rootPath, rootNode, naiveTreeNode);
        System.out.println(naiveTreeNode);
    }
}
